<?php

namespace app\api\controller\user;

use app\api\controller\ApiController;
use app\api\service\DiyService;
use app\api\service\UserService;
use app\api\validate\user\UserValidate;
use think\response\Json;

class User extends ApiController
{
    /**
     * 获取个人中心样式
     * @return Json
     */
    public function getCenterStyle()
    {
        $result = DiyService::detail(['name' => 'person_center', 'type' => 1]);
        return $this->data($result);
    }

    /**
     * 获取用户信息
     * @return Json
     */
    public function getUserInfo()
    {
        $uid = $this->userId;
        return $this->data(UserService::getInfo($uid));
    }

    /**
     * 更新手机号
     * @return Json
     */
    public function updateMobile()
    {
        $uid = $this->userId;
        $params = (new UserValidate())->post()->checkParam('updateMobileCheck', ['id' => $uid]);
        $res = UserService::updateMobile($uid, $this->userInfo['mobile'], $params['mobile'], $params['code']);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(UserService::getError());
        }
    }

    /**
     * 更新密码
     * @return Json
     */
    public function updatePassword()
    {
        $uid = $this->userId;
        $params = (new UserValidate())->post()->checkParam('updatePasswordCheck', ['id' => $uid]);
        $res = UserService::updatePassword($uid, $params['new_password']);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(UserService::getError());
        }
    }

    /**
     * 更新头像
     * @return Json
     */
    public function updateAvatar()
    {
        $uid = $this->userId;
        $params = (new UserValidate())->post()->checkParam('updateAvatarCheck', ['id' => $uid]);
        $res = UserService::updateAvatar($uid, $params['avatar']);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(UserService::getError());
        }
    }

    /**
     * 更新昵称
     * @return Json
     */
    public function updateNickname()
    {
        $uid = $this->userId;
        $params = (new UserValidate())->post()->checkParam('updateNicknameCheck', ['id' => $uid]);
        $res = UserService::updateNickname($uid, $params['nickname']);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(UserService::getError());
        }
    }

    /**
     * 更新邮箱
     * @return Json
     */
    public function updateEmail()
    {
        $uid = $this->userId;
        $params = (new UserValidate())->post()->checkParam('updateEmailCheck', ['id' => $uid]);
        $res = UserService::updateEmail($uid, $params['email']);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(UserService::getError());
        }
    }

    /**
     * 更新性别
     * @return Json
     */
    public function updateSex()
    {
        $uid = $this->userId;
        $params = (new UserValidate())->post()->checkParam('updateSexCheck', ['id' => $uid]);
        $res = UserService::updateSex($uid, $params['sex']);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(UserService::getError());
        }
    }
}